using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Db;
using ApiTest.Api.Repository;
using ApiTest.Api.Params;
using Microsoft.Extensions.Configuration;
using ApiTest.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Admin3000.Backend.Api.Params;

namespace ApiTest.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class ClassifyController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Classify> _classifyRepository;
        private TokenParameter _tokenParameter;

        public ClassifyController(IConfiguration configuration, IRepository<Classify> classifyRepository)
        {
            _configuration = configuration;
            _classifyRepository = classifyRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }

        //列出分类列表
        [HttpGet]
        public dynamic Get()
        {
            var classify = _classifyRepository.Table.ToList();
            var res = new
            {
                Code = 1000,
                Data = new { Data = classify },
                Msg = "获取列表成功^_^"
            };

            return JsonHelper.Serialize(res);
        }


        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var classify = _classifyRepository.GetById(id);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = classify,
                Msg = "查询指定用户成功"
            });
        }

        [HttpPost]
        public dynamic Post(CreateClassify newClassify)
        {
            var classifyname = newClassify.Classifyname.Trim();
            var Classify = new Classify
            {
                Classifyname = newClassify.Classifyname,
            };
            _classifyRepository.Insert(Classify);
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = Classify,
                Msg = "添加用户成功"
            });
        }
        [HttpPut("{id}")]
        public dynamic Put(int id, CreateClassify updateClassify)
        {
            var Classifyname = updateClassify.Classifyname.Trim();
            var classify = _classifyRepository.GetById(id);
            if (classify == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "查询不到该用户，无法更新，请重试"
                });
            }

            classify.Classifyname = updateClassify.Classifyname;
            // user.IsActived= updateUser.IsActived;

            _classifyRepository.Update(classify);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = classify,
                Msg = "更新成功"
            });


        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _classifyRepository.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除成功"
            };


        }
    }

}
